//----------------------------------------------------------------------------
// textcmd.cpp
//
// display display text in read-only editor
//
// Copyright (C) 2011 Neil Butterworth
//----------------------------------------------------------------------------

#include <windows.h>
#include <string>
#include <iostream>
#include "command.h"
#include "cmdline.h"
#include "util.h"
#include "resource.h"
#include "error.h"

using namespace std;

namespace Swine {

//----------------------------------------------------------------------------
// Command & option names
//----------------------------------------------------------------------------

const char * const TEXT_CMD 		= "text";
const char * const CAPTION_OPT 	= "caption";

//----------------------------------------------------------------------------
// Help
//----------------------------------------------------------------------------

const char * const TEXT_HELP =
	"display text read from standard input in read-only multi-line editor\n"
	"options:\n"
	" --caption     specify message box caption (default is 'Text') \n";


//----------------------------------------------------------------------------
// Command class and registration
//----------------------------------------------------------------------------

class TextCmd : public Command {

	public:

		TextCmd() : Command( TEXT_CMD, TEXT_HELP ) {}

		BOOL ProcessControlMessages( HWND wnd, int ctrl, int msg ) const;
		int Execute( CmdLine & cl ) const;
		void InitDialog( HWND dlg, CmdLine & cl ) const;
};

static AddCommand <TextCmd> cmd1_;

//----------------------------------------------------------------------------
// Need to explicitly load library if using rich edit - microsoft madness!
//----------------------------------------------------------------------------

int TextCmd :: Execute( CmdLine & cl ) const {
	LoadLibrary( "Riched32.dll" );
	DoDialog( IDD_TEXTDLG, cl );
	return 0;
}

//----------------------------------------------------------------------------
//  Return if user hit OK or cancel
//----------------------------------------------------------------------------

BOOL TextCmd :: ProcessControlMessages( HWND hwnd, int ctrl, int msg ) const {
	if ( ctrl == IDCANCEL && msg == BN_CLICKED ) {
		Reply( "cancel" );
		DestroyWindow( hwnd );
		return TRUE;
	}
	else if ( ctrl == IDOK && msg == BN_CLICKED ) {
		Reply( "ok" );
		DestroyWindow( hwnd );
		return TRUE;
	}
	else {
		return TRUE;
	}
}

//----------------------------------------------------------------------------
// Set caption from command line. Read standard input and populate list.
// Currently we leak the font at the end of the program. Important?
//----------------------------------------------------------------------------

void TextCmd :: InitDialog( HWND dlg, CmdLine & cl ) const {

	HWND te = MustGetControl( dlg, IDC_TEXTDISPLAY );
	SetWindowText( te, "" );
	HFONT font = CreateFont( 14, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
								OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
								FIXED_PITCH, "Courier New" );

	SendMessage( te, WM_SETFONT, (WPARAM) font, 0  );
	ShowWindow( te, SW_SHOW );

	string caption = cl.ExtractOption( CAPTION_OPT, "Text" );
	cl.MustBeEmpty();
	SetWindowText( dlg, caption.c_str() );

	string line, lines;
	int n = 0;

	while( std::getline( cin, line ) ) {
		n++;
		lines += line + "\r\n";
	}
	if ( n != 0 ) {
		SetWindowText( te, lines.c_str() );
		SetFocus( te );
	}
}


} // namespace
